/*
 * File:    Include.java
 * Created: 26-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2009, Bitgate Mobile, LLC.  All Rights Reserved.
 *
 * software@bitgatemobile.com
 */

package com.webplasm.render.tags.u;

import java.util.Arrays;
import java.util.List;

import com.webplasm.context.RenderContext;
import com.webplasm.render.convert.Convert;
import com.webplasm.render.engine.RenderEngine;
import com.webplasm.render.engine.RenderException;
import com.webplasm.render.tags.TagInterface;
import com.webplasm.render.tags.WrappedNode;

/**
 * Provides a way to include a file into the rendered page.
 * 
 * @author kenji
 * @since Webplasm 1.0
 */
public class Include extends WrappedNode implements TagInterface {
	private static final String DESCRIPTION = "Includes a file for rendering.";

	public String getNamespace() {
		return "u";
	}
	
	public List<String> getAttributes() {
		return Arrays.asList("file");
	}
	
	public List<String> getManagedNodes() {
		return null;
	}

	public String getDescription() {
		return DESCRIPTION;
	}

	@Override
	public StringBuffer render(RenderContext rContext) throws RenderException {
		if (elements.get("file") == null) {
			throw new RenderException("u:include requires a file to be set.");
		}
		
		String convertedPage = Convert.convertOutput(elements.get("file"));
		
		if (convertedPage == null) {
			throw new RenderException("Unable to render page, Webplasm could not convert the output.");
		}
		
		RenderEngine renderEngine = new RenderEngine();
		
		return renderEngine.render(convertedPage.getBytes(), rContext);
	}
}